/**
 * 角色配置和权限管理
 */

// 角色定义
export const roleConfig = {
  // 角色列表
  roles: {
    // 销售角色
    sales_employee: {
      name: '销售员工',
      code: 'sales_employee',
      permissions: [
        'crm:customer:view',
        'crm:customer:create',
        'crm:customer:update',
        'crm:contact:view',
        'crm:contact:create',
        'crm:contact:update',
        'crm:project:view',
        'crm:project:create',
        'crm:project:update',
        'oms:order:view',
        'oms:order:create'
      ],
      modules: ['crm', 'oms']
    },
    sales_manager: {
      name: '销售管理',
      code: 'sales_manager',
      permissions: [
        'crm:customer:view',
        'crm:customer:create',
        'crm:customer:update',
        'crm:customer:delete',
        'crm:contact:view',
        'crm:contact:create',
        'crm:contact:update',
        'crm:contact:delete',
        'crm:project:view',
        'crm:project:create',
        'crm:project:update',
        'crm:project:delete',
        'oms:order:view',
        'oms:order:create',
        'oms:order:update',
        'oms:order:delete',
        'oms:design:view',
        'oms:design:create',
        'oms:design:update'
      ],
      modules: ['crm', 'oms']
    },

    // 仓库角色
    warehouse_employee: {
      name: '仓库员工',
      code: 'warehouse_employee',
      permissions: [
        'wms:inventory:view',
        'wms:material:view',
        'wms:inbound:view',
        'wms:inbound:create',
        'wms:outbound:view',
        'wms:outbound:create'
      ],
      modules: ['wms']
    },
    warehouse_manager: {
      name: '仓库管理',
      code: 'warehouse_manager',
      permissions: [
        'wms:inventory:view',
        'wms:inventory:update',
        'wms:material:view',
        'wms:material:create',
        'wms:material:update',
        'wms:material:delete',
        'wms:inbound:view',
        'wms:inbound:create',
        'wms:inbound:update',
        'wms:inbound:delete',
        'wms:outbound:view',
        'wms:outbound:create',
        'wms:outbound:update',
        'wms:outbound:delete'
      ],
      modules: ['wms']
    },

    // 生产角色
    production_employee: {
      name: '生产员工',
      code: 'production_employee',
      permissions: [
        'production:task:view',
        'production:progress:update',
        'production:quality:view'
      ],
      modules: ['production']
    },
    production_manager: {
      name: '生产管理',
      code: 'production_manager',
      permissions: [
        'production:task:view',
        'production:task:create',
        'production:task:update',
        'production:task:delete',
        'production:task:start',
        'production:task:complete',
        'production:progress:view',
        'production:progress:update',
        'production:plan:view',
        'production:plan:create',
        'production:plan:update',
        'production:quality:view',
        'production:quality:create',
        'production:quality:update'
      ],
      modules: ['production']
    }
  },

  // 模块定义
  modules: {
    crm: {
      key: 'crm',
      name: 'CRM客户管理',
      icon: 'cuIcon-group',
      color: 'blue',
      description: '客户关系管理系统',
      pages: [
        { path: '/pages/crm/customer/list', name: '客户列表' },
        { path: '/pages/crm/contact/list', name: '联系人管理' },
        { path: '/pages/crm/project/list', name: '项目管理' }
      ]
    },
    oms: {
      key: 'oms',
      name: 'OMS订单管理',
      icon: 'cuIcon-form',
      color: 'green',
      description: '订单管理系统',
      pages: [
        { path: '/pages/oms/order/list', name: '订单列表' },
        { path: '/pages/oms/design/list', name: '设计管理' }
      ]
    },
    wms: {
      key: 'wms',
      name: 'WMS仓库管理',
      icon: 'cuIcon-box',
      color: 'orange',
      description: '仓库管理系统',
      pages: [
        { path: '/pages/wms/inventory/list', name: '库存管理' },
        { path: '/pages/wms/material/list', name: '物料管理' },
        { path: '/pages/wms/inbound/list', name: '入库管理' },
        { path: '/pages/wms/outbound/list', name: '出库管理' }
      ]
    },
    production: {
      key: 'production',
      name: '生产管理',
      icon: 'cuIcon-settings',
      color: 'purple',
      description: '生产管理系统',
      pages: [
        { path: '/pages/production/task/list', name: '生产任务' },
        { path: '/pages/production/plan/list', name: '生产计划' },
        { path: '/pages/production/quality/list', name: '质量检验' }
      ]
    }
  },

  // 快捷操作定义
  quickActions: {
    sales_employee: [
      { name: '新增客户', icon: 'cuIcon-add', path: '/pages/crm/customer/create', color: 'blue' },
      { name: '客户列表', icon: 'cuIcon-group', path: '/pages/crm/customer/list', color: 'blue' },
      { name: '新增订单', icon: 'cuIcon-form', path: '/pages/oms/order/create', color: 'green' },
      { name: '我的项目', icon: 'cuIcon-discover', path: '/pages/crm/project/list', color: 'purple' }
    ],
    sales_manager: [
      { name: '客户统计', icon: 'cuIcon-chart', path: '/pages/crm/customer/stats', color: 'blue' },
      { name: '订单统计', icon: 'cuIcon-form', path: '/pages/oms/order/stats', color: 'green' },
      { name: '销售报表', icon: 'cuIcon-copy', path: '/pages/report/sales', color: 'orange' },
      { name: '团队管理', icon: 'cuIcon-group', path: '/pages/team/manage', color: 'purple' }
    ],
    warehouse_employee: [
      { name: '扫码入库', icon: 'cuIcon-scan', path: '/pages/wms/inbound/scan', color: 'green' },
      { name: '扫码出库', icon: 'cuIcon-scan', path: '/pages/wms/outbound/scan', color: 'orange' },
      { name: '库存查询', icon: 'cuIcon-search', path: '/pages/wms/inventory/search', color: 'blue' },
      { name: '盘点任务', icon: 'cuIcon-check', path: '/pages/wms/inventory/check', color: 'purple' }
    ],
    warehouse_manager: [
      { name: '库存统计', icon: 'cuIcon-chart', path: '/pages/wms/inventory/stats', color: 'blue' },
      { name: '入库管理', icon: 'cuIcon-add', path: '/pages/wms/inbound/list', color: 'green' },
      { name: '出库管理', icon: 'cuIcon-delete', path: '/pages/wms/outbound/list', color: 'orange' },
      { name: '物料管理', icon: 'cuIcon-box', path: '/pages/wms/material/list', color: 'purple' }
    ],
    production_employee: [
      { name: '我的任务', icon: 'cuIcon-list', path: '/pages/production/task/my', color: 'blue' },
      { name: '进度汇报', icon: 'cuIcon-edit', path: '/pages/production/progress/report', color: 'green' },
      { name: '质量检验', icon: 'cuIcon-check', path: '/pages/production/quality/check', color: 'orange' },
      { name: '工艺查看', icon: 'cuIcon-discover', path: '/pages/production/process/view', color: 'purple' }
    ],
    production_manager: [
      { name: '生产计划', icon: 'cuIcon-calendar', path: '/pages/production/plan/list', color: 'blue' },
      { name: '任务管理', icon: 'cuIcon-settings', path: '/pages/production/task/list', color: 'green' },
      { name: '生产统计', icon: 'cuIcon-chart', path: '/pages/production/stats', color: 'orange' },
      { name: '质量报表', icon: 'cuIcon-copy', path: '/pages/production/quality/report', color: 'purple' }
    ]
  }
}

/**
 * 根据用户身份获取角色信息
 */
export function getRoleByUserIdentity(userIdentity) {
  const roleMap = {
    1: 'sales_employee',
    2: 'sales_manager',
    3: 'warehouse_employee',
    4: 'warehouse_manager',
    5: 'production_employee',
    6: 'production_manager'
  }
  
  const roleCode = roleMap[userIdentity]
  return roleCode ? roleConfig.roles[roleCode] : null
}

/**
 * 检查权限
 */
export function hasPermission(userRole, permission) {
  const role = roleConfig.roles[userRole]
  return role ? role.permissions.includes(permission) : false
}

/**
 * 获取用户可访问的模块
 */
export function getAccessibleModules(userRole) {
  const role = roleConfig.roles[userRole]
  if (!role) return []
  
  return role.modules.map(moduleKey => roleConfig.modules[moduleKey]).filter(Boolean)
}

/**
 * 获取用户快捷操作
 */
export function getQuickActions(userRole) {
  return roleConfig.quickActions[userRole] || []
}
